package com.xunwulian.cmeb.service;

import com.google.common.collect.Lists;
import com.xunwulian.base.constant.*;
import com.xunwulian.base.entity.AppEntity;
import com.xunwulian.base.exception.ErrorCodeException;
import com.xunwulian.base.util.DateUtil;
import com.xunwulian.base.util.EnumUtils;
import com.xunwulian.cmeb.command.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;

public class CmebUtil {

    /**
     * 查询余额
     *
     * @param msisdn
     * @return
     */
    public Double queryBalance(String msisdn, AppEntity appEntity) throws ErrorCodeException {

        Receiver receiver = new Receiver(appEntity);

        // 查询余额
        Command command = new BalanceCommand(receiver, msisdn);
        Invoker invoker = new Invoker(command);

        return (Double) invoker.action();
    }

    // 查询GPRS使用量，日期为当前日期的前一天
    public String queryUsedGprs(String msisdn, AppEntity appEntity) throws ErrorCodeException {

        Receiver receiver = new Receiver(appEntity);

        Command command = new BatchGprsUsedCommand(receiver, Lists.newArrayList(msisdn),
                DateUtil.DateToString(DateUtil.addDay(new Date(), -1), "yyyyMMdd"));
        Invoker invoker = new Invoker(command);
        HashMap<String, String> map = (HashMap<String, String>) invoker.action();

        return map.get(msisdn);
    }

    // 短信使用量查询
    public String queryUsedSms(String msisdn, AppEntity appEntity) throws ErrorCodeException {

        Receiver receiver = new Receiver(appEntity);

        Command command = new BatchSmsUsedCommand(receiver, Lists.newArrayList(msisdn),
                DateUtil.DateToString(DateUtil.addDay(new Date(), -1), "yyyyMMdd"));
        Invoker invoker = new Invoker(command);
        HashMap<String, String> map = (HashMap<String, String>) invoker.action();

        return map.get(msisdn);
    }


    /***
     * 卡相关信息
     */
    public CmebEntity queryCardInfo(String msisdn, AppEntity appEntity) throws ErrorCodeException {

        Receiver receiver = new Receiver(appEntity);

        /**
         * 除了开关机状态，其他
         * */
        // 用户当月短信查询
        Command command = new SmsUsedCommand(receiver, msisdn);
        Invoker invoker = new Invoker(command);
        Integer smsused = (Integer) invoker.action();

        // 用户余额
        BalanceCommand balanceCommand = new BalanceCommand(receiver, msisdn);
        invoker = new Invoker(balanceCommand);
        String balance = invoker.action().equals(ConstantBean.FAIL_VALUE_DOUBLE) ? "未知" : String.valueOf(invoker.action());

        // 当月GPRS使用量
        command = new GprsUsedCommand(receiver, msisdn);
        invoker = new Invoker(command);
        String gprsused = invoker.action().equals(ConstantBean.FAIL_VALUE_LONG) ? "未知" : transformGPRS((Long) invoker.action());

        // GPRS 连接信息
        command = new GprsInfoCommand(receiver, msisdn);
        invoker = new Invoker(command);
        GprsInfoEntity gprsInfoEntity = (GprsInfoEntity) invoker.action();
        String gprsstatus = EnumUtils.getEnumByType(GprsOnlineStatus.class, gprsInfoEntity.getGprsstatus()).getDesc
                ();
        String rat = EnumUtils.getEnumByType(RatType.class, gprsInfoEntity.getRat()).getDesc();
        String ip = gprsInfoEntity.getIp();
        String apn = gprsInfoEntity.getApn();

        // 用户卡状态
        String userstatus = "未知";
        try {
            command = new UserStatusCommand(receiver, msisdn);
            invoker = new Invoker(command);
            String userStatus = (String) invoker.action();
            userstatus = EnumUtils.getEnumByType(CmebCardStatus.class, userStatus).getDesc();
        } catch (ErrorCodeException e) {
            userstatus = e.getMessage();
        }
        // 开关机状态
        String onoff = "未知";
        try {
            command = new OnOrOffCommand(receiver, msisdn);
            invoker = new Invoker(command);
            Integer onOffStatus = (Integer) invoker.action();
            onoff = EnumUtils.getEnumByType(OnAndOffStatus.class, onOffStatus).getDesc();

        } catch (ErrorCodeException e) {
            onoff = e.getMessage();
        }

        CmebEntity cmeb = new CmebEntity(smsused, balance, gprsused, onoff, userstatus, gprsstatus,
                rat, ip, apn);

        return cmeb;
    }

    private String transformGPRS(long gprs) {
        long GB = 1024 * 1024 * 1024;
        long MB = 1024 * 1024;
        long KB = 1024;
        if (gprs > GB) {
            return String.valueOf(gprs % GB) + " GB";
        } else if (gprs > MB) {
            return String.valueOf(gprs / MB) + " MB";
        } else {
            return String.valueOf(gprs / KB) + " KB";
        }

    }

}
